class Solution {
    public int[] getLeastNumbers(int[] arr, int k) {
        if (arr.length == 0 || k == 0) {
            return new int[0];  
        }
        PriorityQueue<Integer> queue = new PriorityQueue<>((o1, o2) -> Integer.compare(o2, o1));
        for (Integer item : arr) {
            if (queue.size() == k) {
                if (queue.peek() > item) {
                    queue.poll();
                    queue.add(item);
                }
            } else {
                queue.add(item);
            }
        }
        int[] res = new int[k];
        for (int i = 0; i < res.length; i++) {
            res[i] = queue.poll();
        }
        return res;
    }
}